package com.ruoyi.bizsys.controller;

import com.ruoyi.bizsys.domain.AwardAgent;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.bizsys.service.IAwardAgentService;
import com.ruoyi.bizsys.service.IUserActivityGroupService;
import com.ruoyi.bizsys.vo.AwardGroupVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DesensitizeUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 代理人Controller
 *
 * @author african-loan
 * @date 2022-06-24
 */
@AllArgsConstructor
@RestController
@RequestMapping("/award/agent")
public class AwardAgentController extends BaseController {

    private IAwardAgentService awardAgentService;

    private IUserActivityGroupService userActivityGroupService;

    private IAppUserService appUserService;

    /**
     * 查询奖励活动列表
     */
    @PreAuthorize("@ss.hasPermi('bizsys:agent:list')")
    @GetMapping("/list")
    public TableDataInfo list(AwardAgent awardAgent) {
        startPage();
        TableDataInfo tableDataInfo = getDataTable(awardAgentService.list(awardAgent));
        if (!CollectionUtils.isEmpty(tableDataInfo.getRows())) {
            List<AwardAgent> awardAgentList = (List<AwardAgent>) tableDataInfo.getRows();
            awardAgentList.stream().forEach(obj -> {
                obj.setPhone(DesensitizeUtils.around(obj.getPhone(), 3, 4));
                obj.setRecommendPhone(DesensitizeUtils.around(obj.getRecommendPhone(), 3, 4));
            });
        }
        return tableDataInfo;
    }


    /**
     * 批量更新分组
     *
     * @param
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('bizsys:agent:updateGroup')")
    @PostMapping("/updateGroup")
    @ResponseBody
    public AjaxResult updateGroup(@RequestBody @Valid AwardGroupVo awardGroupVo) {
        userActivityGroupService.saveOrUpdateByUserIds(awardGroupVo);
        return AjaxResult.success(true);
    }


    /**
     * Excel批量更新or新增分组
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/updateGroup/import")
    @PreAuthorize("@ss.hasPermi('bizsys:agent:updateGroupImport')")
    public AjaxResult importFile(@RequestParam("file") MultipartFile file) throws Exception {
        List<AwardGroupVo> list = awardAgentService.fileParseList(file);
        userActivityGroupService.updateBatch(list);
        return AjaxResult.success(true);
    }


    /**
     * 冻结用户邀请资格
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/freeze/{userId}")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('bizsys:agent:updateGroupImport')")
    public AjaxResult freezeInviteState(@PathVariable String userId,@RequestBody AwardAgent awardAgent) {
        return AjaxResult.success(appUserService.freezeInviteState(userId,awardAgent.getInviteCodeStatus()));
    }


}
